package com.example.demo.service.impl;

import com.example.demo.dto.DepartmentDTO;
import com.example.demo.entity.Department;
import com.example.demo.mapper.DepartmentMapper;
import com.example.demo.request.CreateDepartmentRequest;
import com.example.demo.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.exception.BusinessException;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public List<DepartmentDTO> getDepartments(int page, int size) {
        // 实际开发中应该使用分页查询
        List<Department> departments = departmentMapper.selectAll();
        return departments.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    @Override
    public long countDepartments() {
        return departmentMapper.count();
    }

    @Override
    public DepartmentDTO getDepartmentById(Integer id) {
        Department department = departmentMapper.selectById(id);
        return convertToDTO(department);
    }

    @Override
    public Department createDepartment(CreateDepartmentRequest request) {
        // 检查部门中文名是否重复
        Department existingByName = departmentMapper.selectByDeptName(request.getDeptName());
        if (existingByName != null) {
            throw new BusinessException("部门名称'" + request.getDeptName() + "'已存在");
        }
        
        // 检查部门英文名是否重复
        Department existingByEnName = departmentMapper.selectByDeptEnName(request.getDeptEnName());
        if (existingByEnName != null) {
            throw new BusinessException("部门英文名'" + request.getDeptEnName() + "'已存在");
        }
        
        Department department = new Department();
        department.setDeptName(request.getDeptName());
        department.setDeptEnName(request.getDeptEnName());
        department.setDeptDesc(request.getDeptDesc());
        departmentMapper.insert(department);
        return department;
    }

    @Override
    public void updateDepartment(Integer id, CreateDepartmentRequest request) {
        // 检查部门中文名是否重复（排除当前部门）
        Department existingByName = departmentMapper.selectByDeptNameExcludeId(request.getDeptName(), id);
        if (existingByName != null) {
            throw new BusinessException("部门名称'" + request.getDeptName() + "'已存在");
        }
        
        // 检查部门英文名是否重复（排除当前部门）
        Department existingByEnName = departmentMapper.selectByDeptEnNameExcludeId(request.getDeptEnName(), id);
        if (existingByEnName != null) {
            throw new BusinessException("部门英文名'" + request.getDeptEnName() + "'已存在");
        }
        
        Department department = departmentMapper.selectById(id);
        department.setDeptName(request.getDeptName());
        department.setDeptEnName(request.getDeptEnName());
        department.setDeptDesc(request.getDeptDesc());
        departmentMapper.updateById(department);
    }

    @Override
    public void deleteDepartment(Integer id) {
        departmentMapper.deleteById(id);
    }
    
    @Override
    public List<DepartmentDTO> searchDepartmentsByName(String name) {
        List<Department> departments = departmentMapper.selectByNameLike(name);
        return departments.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    private DepartmentDTO convertToDTO(Department department) {
        DepartmentDTO dto = new DepartmentDTO();
        dto.setDeptId(department.getDeptId());
        dto.setDeptName(department.getDeptName());
        dto.setDeptEnName(department.getDeptEnName());
        dto.setDeptDesc(department.getDeptDesc());
        return dto;
    }
}